package lib.quasar.widget.button;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.v7.widget.AppCompatButton;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;

import lib.quasar.widget.R;

/**
 * description: 圆形按钮
 * created by kalu on 2018/4/23 13:34
 */
public class CircleButton extends AppCompatButton {

    private boolean isTouch = false;
    private boolean isEnable = true;

    public CircleButton(Context context) {
        super(context);
    }

    public CircleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void draw(Canvas canvas) {

        setBackgroundColor(Color.TRANSPARENT);

        TextPaint paint = getPaint();
       // paint.setFakeBoldText(true);
        float temp = getWidth() / 2f;
        paint.setColor(getResources().getColor(isTouch ? R.color.color_bg_theme_press : R.color.color_bg_theme));
        canvas.drawCircle(temp, temp, temp, paint);

        paint.setColor(Color.WHITE);
        super.draw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!isEnable) return super.onTouchEvent(event);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isTouch = true;
                postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_POINTER_UP:
                isTouch = false;
                postInvalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    public void setEnable(boolean enable) {
        isEnable = enable;
    }
}
